package com.ciquan.server.controller;

import com.ciquan.server.bean.PushMessage;
import com.ciquan.server.bean.UserInfo;
import com.ciquan.server.service.PhoneCodeService;
import com.ciquan.server.service.PushMessageService;
import com.ciquan.server.service.UserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by bjliwei2014 on 2015/3/9.
 */
@Controller
@RequestMapping("/apply")
public class ApplyController extends AbstractController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private PushMessageService pushMessageService;

    @Resource
    private PhoneCodeService phoneCodeService;

    @RequestMapping("/list")
    public ModelAndView list(ModelMap modelMap) {
        List<UserInfo> userInfos = userInfoService.selectApplyUser();
        modelMap.put("userInfos", userInfos);
        return new ModelAndView("applyList", modelMap);
    }

    @RequestMapping("/applyUser")
     public ModelAndView applyUser(HttpServletRequest request) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        userInfoService.applyUser(id);

        UserInfo userInfo = userInfoService.selectById(id);
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTitle("瓷圈");
        pushMessage.setContent("已通过申请，进入【我的藏品】发布藏品吧");
        pushMessage.setType("1");
        pushMessageService.sendMessage(userInfo, pushMessage);
        phoneCodeService.applyUser(userInfo.getMobile());
        return new ModelAndView("redirect:/apply/list");
    }

    @RequestMapping("/applyRefuse")
    public ModelAndView applyRefuse(HttpServletRequest request) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        userInfoService.applyRefuse(id);

        UserInfo userInfo = userInfoService.selectById(id);
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTitle("瓷圈");
        pushMessage.setContent("未通过申请，完善头像姓名简介，重新申请");
        pushMessage.setType("2");
        pushMessageService.sendMessage(userInfo, pushMessage);
        phoneCodeService.applyRefuse(userInfo.getMobile());
        return new ModelAndView("redirect:/apply/list");
    }

}
